package model.room
{
	import core.Model;
	
	import events.TableEvent;
	
	import flash.events.EventDispatcher;
	
	import global.GameState;
	
	import model.card.ICardData;
	import model.result.RoundResultData;
	import model.role.IRoleData;
	import model.sit.ISitData;
	import model.sit.SitData;
	
	import utils.DisplayTool;
	import utils.ai.CardTools;
	
	import view.card.Card;
	import view.card.ICard;

	public class TableModel extends EventDispatcher
	{
		protected var _model:Model = Model.getInstance();
		
		private static var _lastShowCardDatas:Array;
		private var currentBankerSitId:int = -1;
		private var sitCards:Array;
		
		public function TableModel()
		{
			sitCards = [];
		}
		
		public function get lastShowCardDatas():Array
		{
			return CardTools.cloneCardDatas(_lastShowCardDatas);
		}
		
		public function updateShowCards(sitId:int,cardDatas:Array):void
		{
			CardTools.sortCards(cardDatas);
			
			var card:ICard;
			var cards:Array = [];
			var cardData:ICardData;
			
			clearSitShowCards(sitId);
			
			for (var i:int=0; i<cardDatas.length; i++)
			{
				cardData = cardDatas[i];
				card = new Card();
				
				if (sitId == 4)
				{
					card.self.x = GameState.sitCardXYS[sitId][0] - cardDatas.length * 15 + i* 15;
				}
				else
				{
					card.self.x = GameState.sitCardXYS[sitId][0] + i* 15;
				}
				card.self.y = GameState.sitCardXYS[sitId][1];
				card.updateData(cardData);
				cards.push(card);
			}
			
			sitCards[sitId] = cards;
			_lastShowCardDatas = cardDatas;
			
			var tableEvent:TableEvent = new TableEvent(TableEvent.SHOW_CARDS);
			tableEvent.showCards = cards;
			dispatchEvent(tableEvent);
		}
		
		public function clearSitShowCards(sitId:int):void
		{
			var card:ICard;
			if (sitCards[sitId] != null)
			{
				for (var n:String in sitCards[sitId])
				{
					card = sitCards[sitId][n];
					
					DisplayTool.removeChild(card.self.parent,card.self);
					card.destroy();
				}
			}
			sitCards[sitId] = null;
		}
		
		public function updateBanker(sitId:int):void
		{
			currentBankerSitId = sitId;
			var tableEvent:TableEvent = new TableEvent(TableEvent.BANKER_CHANGE);
			tableEvent.newBankerSitId = sitId;
			dispatchEvent(tableEvent);
		}
		
		public function playerSit(roleData:IRoleData):void
		{
			var sitData:ISitData = new SitData();
			sitData.roleData = roleData;
			
			_model.sitModel.addSit(sitData);
		}
		
		public function updateRoomInfo(roomData:RoomData):void
		{
			var tableEvent:TableEvent = new TableEvent(TableEvent.ROOM_INFO);
			tableEvent.roomData = roomData;
			dispatchEvent(tableEvent);
		}
		
		public function updateSysMsg(content:String):void
		{
			var tableEvent:TableEvent = new TableEvent(TableEvent.SYSTEM_MSG);
			tableEvent.msgContent = content;
			dispatchEvent(tableEvent);
		}
		
		public function updateChatMsg(content:String):void
		{
			var tableEvent:TableEvent = new TableEvent(TableEvent.CHAT_MSG);
			tableEvent.msgContent = content;
			dispatchEvent(tableEvent);
		}
		
		public function updateMultiRemainCards(remain:String,multi:String):void
		{
			var tableEvent:TableEvent = new TableEvent(TableEvent.REMAIN_MULTI);
			tableEvent.remain = remain;
			tableEvent.multi = multi;
			dispatchEvent(tableEvent);
		}
		
		public function updateResult(roundResultData:RoundResultData):void
		{
			var tableEvent:TableEvent = new TableEvent(TableEvent.ROUND_RESULT);
			tableEvent.roundResultData = roundResultData;
			dispatchEvent(tableEvent);
		}
		
		public function updateTableEffect(effectId:String,soundId:String):void
		{
			var tableEvent:TableEvent = new TableEvent(TableEvent.TABLE_ANIMATION);
			tableEvent.effectId = effectId;
			tableEvent.soundId = soundId;
			dispatchEvent(tableEvent);
		}
		
		//返回大厅请求
		public function gotoHall():void
		{
			_model.netRemote.goHall();
			
		}
		
		public function updateGotoHall():void
		{
			var tableEvent:TableEvent = new TableEvent(TableEvent.GO_TO_HALL);
			dispatchEvent(tableEvent);
			
			//_model.loginModel.loginAccess();
		}
		
		public function clearRoundShowCards():void
		{
			_lastShowCardDatas = null;
			sitCards.length = 0;
			var tableEvent:TableEvent = new TableEvent(TableEvent.CLEAR_ROUND_CARDS);
			dispatchEvent(tableEvent);
		}
		
		public function clear():void
		{
			this.clearRoundShowCards();
		}
	}
}